package com.zaoke.web.component.controller;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zaoke.web.bo.order.OrderQuery;
import com.zaoke.web.bo.order.OrderQueryRes;
import com.zaoke.web.service.order.OrderService;
import com.zaoke.web.utils.JacksonUtil;
import com.zaoke.web.utils.WebResponse;

@Controller
@RequestMapping("/order/")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@ResponseBody
	@RequestMapping(value="queryOrder.ajax",method = RequestMethod.POST)  
    public WebResponse<ArrayList<OrderQueryRes>> queryUser(HttpServletRequest request, HttpServletResponse response) {
		
		WebResponse<ArrayList<OrderQueryRes>> wr = new WebResponse<ArrayList<OrderQueryRes>>();
		OrderQuery query;
		try {
			query = JacksonUtil.readObj(request.getInputStream(), OrderQuery.class);
			List<OrderQueryRes> orders = orderService.queryOrderInfo(query);
			
			if(orders != null && orders.size() > 0)
				for(OrderQueryRes res : orders)
					res.setPriceStr(new DecimalFormat("0.00").format(res.getPrice()));
			
			ArrayList<OrderQueryRes> dest = new ArrayList<OrderQueryRes>(orders);
			wr.setData(dest);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
    }
	
}
